package org.jeecg.modules.tls.blog.article.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;


import org.jeecg.modules.tls.blog.article.entity.AddArticleDto;
import org.jeecg.modules.tls.blog.article.entity.AddBlogDto;
import org.jeecg.modules.tls.blog.article.entity.BlogArticle;
import org.jeecg.modules.tls.blog.article.service.IBlogArticleService;
import org.jeecg.modules.tls.blog.article.vo.BlogCountVo;
import org.jeecg.modules.tls.blog.blogarticletag.service.IBlogArticleTagService;
import org.jeecg.modules.tls.blog.common.ResponseResult;
import org.jeecg.modules.tls.blog.utils.UploadUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: blog_article
 * @Author: jeecg-boot
 * @Date:   2023-01-06
 * @Version: V1.0
 */
@Api(tags="blog_article")
@RestController
@RequestMapping("/blog/blogArticle")
@Slf4j

public class BlogArticleController extends JeecgController<BlogArticle, IBlogArticleService> {
	@Autowired
	private IBlogArticleService blogArticleService;

	@Autowired
	private IBlogArticleTagService blogArticleTagService;
	
	/**
	 * 分页列表查询
	 *
	 * @param blogArticle
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "blog_article-分页列表查询")
	@ApiOperation(value="blog_article-分页列表查询", notes="blog_article-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BlogArticle>> queryPageList(BlogArticle blogArticle,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BlogArticle> queryWrapper = QueryGenerator.initQueryWrapper(blogArticle, req.getParameterMap());
//		queryWrapper.eq("name","2");
		Page<BlogArticle> page = new Page<BlogArticle>(pageNo, pageSize);
		IPage<BlogArticle> pageList = blogArticleService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param addBlogDto
	 * @return
	 */
	@AutoLog(value = "blog_article-添加")
	@ApiOperation(value="blog_article-添加", notes="blog_article-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AddBlogDto addBlogDto) throws IOException {
		return blogArticleService.addAdminBlog(addBlogDto);
	}
	
	/**
	 *  编辑
	 *
	 * @param addBlogDto
	 * @return
	 */
	@AutoLog(value = "blog_article-编辑")
	@ApiOperation(value="blog_article-编辑", notes="blog_article-编辑")
	@PostMapping(value = "/edit")
	public Result<String> edit(@RequestBody AddBlogDto addBlogDto) {
		return blogArticleService.editAdminBlog(addBlogDto);
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		if(!blogArticleService.removeById(id)){
			return Result.error("删除失败");
		}
		blogArticleTagService.deleteByMap(id);
		return Result.ok("删除成功");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "blog_article-批量删除")
	@ApiOperation(value="blog_article-批量删除", notes="blog_article-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.blogArticleService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "blog_article-通过id查询")
	@ApiOperation(value="blog_article-通过id查询", notes="blog_article-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BlogArticle> queryById(@RequestParam(name="id",required=true) String id) {
		BlogArticle blogArticle = blogArticleService.getById(id);
		if(blogArticle==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(blogArticle);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param blogArticle
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BlogArticle blogArticle) {
        return super.exportXls(request, blogArticle, BlogArticle.class, "blog_article");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BlogArticle.class);
    }


	 @GetMapping("/recentBlog")
	 public ResponseResult rencentBlogList(){
    	 //查询最近博客，显示最近博客
		 ResponseResult result = blogArticleService.rencentBlogList();
		 return result;
	 }

	 @GetMapping("/blogArticleList")
	 public ResponseResult blogArticleList(Integer pageCount,
										   Integer pageSize){
		 return blogArticleService.blogArticleList(pageCount,pageSize);
	 }

	 @GetMapping("/articleCategoryList")
	 public ResponseResult blogArticleCategoryList(Integer pageCount,
												   Integer pageSize,
												   String categoryId){
		 return blogArticleService.blogArticleCategoryList(pageCount,pageSize,categoryId);
	 }

	 @GetMapping("/articleTagList")
	 public ResponseResult blogArticleTagList(Integer pageCount,
												   Integer pageSize,
												   String tagId){
		 return blogArticleService.blogArticleTagList(pageCount,pageSize,tagId);
	 }

	 @GetMapping("/blogDetail")
	 public ResponseResult getArticleDetail(String id){
		 return blogArticleService.getArticleDetail(id);
	 }

	 @PostMapping("/upload")
	 public ResponseResult upload(MultipartFile coverImg,MultipartFile topImg) throws IOException {
		 try {
			 UploadUtils.uploadImage(coverImg);
			 UploadUtils.uploadImage(topImg);
			 return ResponseResult.okResult();
		 } catch (IOException e) {
			 e.printStackTrace();
			 throw new RuntimeException("文件上传上传失败");
		 }

	 }

	 @PostMapping("/addBlog")
	 public ResponseResult addBlog(AddArticleDto articleDto) throws IOException {
		 return blogArticleService.addBlog(articleDto);
	 }

	 @PostMapping("/editBlog")
	 public ResponseResult editBlog(AddArticleDto articleDto) throws IOException {
		 return blogArticleService.editBlog(articleDto);
	 }

	 @GetMapping("/blogSearch")
	 public ResponseResult blogSearch(Integer pageCount,
									  Integer pageSize,
									  String searchCondition) {
		 return blogArticleService.blogSearch(pageCount,pageSize,searchCondition);
	 }

	 @GetMapping("/myBlog")
	 public  ResponseResult myBlog(Integer pageNum,
								   Integer pageSize){
    	return blogArticleService.myBlog(pageNum,pageSize);
	 }

	 @PostMapping(value = "/deleteBlog")
	 public ResponseResult deleteBlog(@RequestBody JSONObject jsonParam) {
		 String blogId = jsonParam.getString("id");
		 if(!blogArticleService.removeById(blogId)){
			 return ResponseResult.errorResult(410,"删除失败！");
		 }
		 blogArticleTagService.deleteByMap(blogId);
		 return ResponseResult.okResult(200,"删除成功！");
	 }

	 @GetMapping(value = "/blogCount")
	 public ResponseResult getBlogCount(){
		 //查询条件
		 LambdaQueryWrapper<BlogArticle> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		 long count = blogArticleService.count(lambdaQueryWrapper);
		 BlogCountVo blogCountVo = new BlogCountVo();
		 blogCountVo.setValue(String.valueOf(count));
		 return ResponseResult.okResult(blogCountVo);
	 }

}
